from selenium.webdriver.common.by import By
from core.base_page import BasePage
from utils.config_util import ConfigUtil


class LaunchPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)
        # 加载元素定位器配置
        self.locators = ConfigUtil.load_page_locators("launch_page")
        # 转换定位器格式为 (By.XXX, value) 元组
        self._convert_locators()
        # 从配置获取登录页URL
        self.login_url = ConfigUtil.get_test_url()

    def _convert_locators(self):
        """将配置中的定位器转换为 Selenium 可识别的元组格式"""
        by_map = {
            "ID": By.ID,
            "NAME": By.NAME,
            "XPATH": By.XPATH,
            "CSS_SELECTOR": By.CSS_SELECTOR,
            "CLASS_NAME": By.CLASS_NAME,
            "TAG_NAME": By.TAG_NAME,
            "LINK_TEXT": By.LINK_TEXT,
            "PARTIAL_LINK_TEXT": By.PARTIAL_LINK_TEXT
        }
        for key in self.locators:
            locator = self.locators[key]
            by = by_map.get(locator["by"].upper(), By.XPATH)
            self.locators[key] = (by, locator["value"])

    # 获取添加支付产品后的信息
    def get_product_info(self):
        product_code = self.get_text(self.locators['product_code'])
        product_name = self.get_text(self.locators['product_name'])
        return product_code, product_name

    def open(self):
        """打开登录页（使用配置的URL）"""
        # 修复：使用BasePage中的open方法而非open_url
        self.open_url(self.login_url)  # 这里调用的是BasePage中的open方法
        return self

    def input_username(self, username):
        self.send_keys(self.locators["username"], username)
        return self

    def input_password(self, password):
        self.send_keys(self.locators["password"], password)
        return self

    def click_login(self):
        self.click(self.locators["login_btn"])
        return self

    # 点击左侧菜单[支付管理]
    def click_pay_manage(self):
        self.click(self.locators['pay_manage'])
        return self

    # 点击[支付产品信息]按钮
    def click_pay_info(self):
        self.click(self.locators['pay_info'])
        return self

    # 点击[添加支付产品]按钮
    def click_add_pay(self):
        self.click(self.locators['add_pay'])
        return self

    # 输入支付产品编码
    def input_productCode(self, code):
        self.send_keys(self.locators['input_productCode'], code)
        return self

    # 输入支付产品名称
    def input_productName(self, name):
        self.send_keys(self.locators['input_productName'], name)
        return self

    # 点击[提交]按钮
    def click_commit01_btn(self):
        self.click(self.locators['commit01_btn'])
        return self

    # 点击刚添加的支付产品“小米手环”的[设置支付方式]按钮
    def click_pay_type(self):
        self.click(self.locators['pay_type'])
        return self

    # 点击[添加支付方式按钮]
    def click_set_pay_type(self):
        self.click(self.locators['set_pay_type'])
        return self

    # 选择支付方式
    def select_payWays(self, pay_ways):
        self.select_by_visible_text(self.locators['payWays'], pay_ways)
        return self

    # 选择支付类型
    def select_payTypeCode(self, pay_type):
        self.select_by_visible_text(self.locators['payTypeCode'], pay_type)
        return self

    # 输入支付费率
    def input_payRate(self, rate):
        self.send_keys(self.locators['payRate'], rate)
        return self

    # 点击[提交]按钮
    def click_commit02_btn(self):
        self.click(self.locators['commit02_btn'])
        return self

    # 点击[支付产品信息]按钮
    def click_pay_info2(self):
        self.click(self.locators['pay_info'])
        return self

    # 点击刚添加的支付产品“小米手环”的[上架]按钮
    def click_on_shop(self):
        self.click(self.locators['on_shop'])
        return self

    # 点击[确认]按钮
    def click_confirm_btn(self):
        self.click(self.locators['confirm_btn'])
        return self

    def get_error_tip(self):
        return self.get_text(self.locators["error_tip"])
